﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MiniX.API.Data;
using MiniX.Models;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Senparc.CO2NET.Extensions;
using MiniX.Utility.Helper;

namespace MiniX.API.Controllers
{
    /// <summary>
    /// 配置信息类，定义了CURD相关操作
    /// </summary>
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class SettingInfoController : BaseController<SettingInfoController>
    {
        /// <summary>
        /// SettingInfoController
        /// </summary>
        public SettingInfoController(MiniXApiContext context, IHttpContextAccessor httpContextAccessor) : base(context, httpContextAccessor)
        {
          
        }

        /// <summary>
        /// 分页显示配置信息数据
        /// </summary>
        /// <param name="page">分页</param>
        /// <param name="limit">每页显示数量</param>
        /// <returns></returns>
        [HttpGet]
        [Route("/api/settinginfo")]
        public async Task<IActionResult> GetByPage(int page, int limit, int parent_id)
        {
            try
            {
                var result = from item in _context.Set<SettingInfo>().Where(t => t.parent_id == parent_id)
                             select new
                             {
                                 item.setting_info_id,
                                 item.info_name,
                                 item.parent_id,
                                 item.sort_num,
                                 item.is_show,
                                 item.is_manage_show,
                                 item.type_id,
                                 type_name = item.type_id > 0 ? _context.mini_setting_types.FirstOrDefault(t => t.type_id == item.type_id)!.type_name : "-",
                                 item.info_value,
                                 info_title = item.info_value, // 兼容老版本
                                 item.info_content,
                                 item.info_path,
                                 item.is_open,
                                 item.ext_link,
                                 item.ext_desc,
                                 item.info_tips,
                                 item.info_remark,
                                 item.created_at,
                                 format_time = item.created_at.ToString("yyyy-MM-dd HH:mm")
                              };

                var list = await result.OrderBy(t => t.sort_num).Skip((page - 1) * limit).Take(limit).ToListAsync();
                var count = result.LongCount();
                if (count <= 0)
                {
                    return Json(new { code = 0, msg = "暂无数据", count = 0 });
                }
                else
                {
                    return Json(new { code = 0, msg = "success", data = list, page = page, limit = limit, count = count, total_page = count / limit });
                }
            }
            catch (Exception ex)
            {
                log.Fatal(ex, " -> GetDataByPage");
                return Json(new { code = 1, errcode = (int)ENUMHelper.ExceptionType.List, errmsg = ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取配置信息详情
        /// </summary>
        /// <param name="id">配置信息ID</param>
        /// <returns>返回配置信息详情</returns>
        [HttpGet("/api/settinginfo/{id}")]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                log.Information("id=" + id);
                var result = await _context.mini_setting_info.Where(t => t.setting_info_id == id).Select(item => (new
                {
                    item.setting_info_id,
                    item.info_name,
                    item.parent_id,
                    item.sort_num,
                    item.is_show,
                    item.is_manage_show,
                    item.type_id,
                    type_name = item.type_id > 0 ? _context.mini_setting_types.FirstOrDefault(t => t.type_id == item.type_id)!.type_name : "-",
                    item.info_value,
                    item.info_content,
                    item.info_path,
                    item.is_open,
                    item.ext_link,
                    item.ext_desc,
                    item.info_tips,
                    item.info_remark,
                    item.created_at,
                    format_time = item.created_at.ToString("yyyy-MM-dd HH:mm")
                })).FirstOrDefaultAsync();
                if (result == null)
                {
                    return Json(new { code = 0, msg = "数据为空", count = 0 });
                }
                else
                {
                    return Json(new { code = 0, msg = "success", data = result });
                }
            }
            catch (Exception ex)
            {
                log.Fatal(ex, " -> Details");
                return Json(new { code = 1, errcode = (int)ENUMHelper.ExceptionType.Select, errmsg = ex.Message });
            }
        }
    }
}
